#!/usr/bin/env sh

# 选择或输入数据库镜像
DB_Image_Selection()
{
  while [ -z "${DB_IMAGE}" ]; do
    Echo_Blue "选择数据库镜像(${#DB_IMAGES[*]}):"

    # for image in ${DB_IMAGES[*]}
    for ((i=0;i<${#DB_IMAGES[*]};i++))
    do
      echo "${i}) ${DB_IMAGES[i]}"
    done

    read -p "请选择镜像序号, 或输入镜像名称: " DB_IMAGE
    echo

    if [[ ${DB_IMAGE} =~ ^[0-9]+$ ]]; then
      DB_IMAGE=${DB_IMAGES[${DB_IMAGE}]}
    fi
  done

  # 配置镜像
  DB_Config
}

# 数据库配置
DB_Config()
{
  Echo_Green "数据库配置:"
  echo "DB_IMAGE=${DB_IMAGE}"

  # 包名:版本号 repository:version
  image=${DB_IMAGE%%-*}
  repository=${image%:*}
  version=${image#*:}

  # 资源路径
  DB_PATH=${DATA_PATH}/${repository}/${version}

  # DB_DATA
  if [ -z "${DB_DATA}" ]; then
    CACHE=${DB_PATH}/var
    read -p "数据存储路径(default=${CACHE}): " DB_DATA
  else
    echo "DB_DATA=${DB_DATA}"
  fi
  if [ -z "${DB_DATA}" ]; then
    DB_DATA=${CACHE}
  fi
  
  DB_HOST=${repository}
  echo "DB_HOST=${DB_HOST}"

  # DB_PORT
  if [ -z "${DB_PORT}" ]; then
    CACHE=3306
    read -p "数据库端口(default=${CACHE}): " DB_PORT
  else
    echo "DB_PORT=${DB_PORT}"
  fi
  if [ -z "${DB_PORT}" ]; then
    DB_PORT=${CACHE}
  fi
  # 检查端口占用情况
  Check_Port ${DB_PORT}
  if [ ! -z "${ERROR}" ]; then
    Exit_Confirm
  fi

  # DB_USERNAME
  if [ -z "${DB_USERNAME}" ]; then
    CACHE="xbear"
    read -p "数据库账号(default=${CACHE}): " DB_USERNAME
  else
    echo "DB_USERNAME=${DB_USERNAME}"
  fi
  if [ -z "${DB_USERNAME}" ]; then
    DB_USERNAME=${CACHE}
  fi

  # DB_PASSWORD
  if [ -z "${DB_PASSWORD}" ]; then
    CACHE="xbear.org"
    read -p "数据库密码(default=${CACHE}): " DB_PASSWORD
  else
    echo "DB_PASSWORD=${DB_PASSWORD}"
  fi
  if [ -z "${DB_PASSWORD}" ]; then
    DB_PASSWORD=${CACHE}
  fi

  # DB_ROOT_PASSWORD
  if [ -z "${DB_ROOT_PASSWORD}" ]; then
    CACHE=${DB_PASSWORD} 
    read -p "管理员密码(default=${CACHE}): " DB_ROOT_PASSWORD
  else
    echo "DB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}"
  fi
  if [ -z "${DB_ROOT_PASSWORD}" ]; then
    DB_ROOT_PASSWORD=${CACHE}
  fi

  # DB_DATABASE
  if [ -z "${DB_DATABASE}" ]; then
    CACHE="ystmlive"
    read -p "数据库名称(default=${CACHE}): " DB_DATABASE
  else
    echo "DB_DATABASE=${DB_DATABASE}"
  fi
  if [ -z "${DB_DATABASE}" ]; then
    DB_DATABASE=${CACHE}
  fi

  # 确认并保存配置
  DB_Config_Confirm
}

# 确认配置
DB_Config_Confirm()
{
  confirm="y"
  read -p "确认配置(Y/n): " confirm
  if [[ -z "${confirm}" || "${confirm}" == "y" ]]; then
    # 保存全局安装配置
    DB_Config_Update

    # 复制维护脚本
    mkdir -p ${DB_PATH}/sh
    \cp -r ${CurrentDir}/sh/mysql/. ${DB_PATH}/sh/

    # 镜像安装配置
    cat > ${DB_PATH}/install.conf <<EOF
DB_IMAGE=${DB_IMAGE}
DB_PATH=${DB_PATH}
DB_DATA=${DB_DATA}
DB_HOST=${DB_HOST}
DB_PORT=${DB_PORT}
DB_DATABASE=${DB_DATABASE}
DB_USERNAME=${DB_USERNAME}
DB_PASSWORD=${DB_PASSWORD}
DB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
EOF

    Echo_Green "配置已保存！"
    echo
  else
    # 重置
    DB_IMAGE=
    DB_PATH=
    DB_DATA=
    DB_PORT=
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    DB_ROOT_PASSWORD=
    echo
    DB_Image_Selection
  fi
}

# 更新数据库配置
DB_Config_Update()
{
  # install.conf
  sed -i -e "s|DB_IMAGE=.*|DB_IMAGE=${DB_IMAGE}|g;" ${INSTALL_CONF}
  sed -i -e "s|DB_PATH=.*|DB_PATH=${DB_PATH}|g;" ${INSTALL_CONF}
  sed -i -e "s|DB_DATA=.*|DB_DATA=${DB_DATA}|g;" ${INSTALL_CONF}
  # sed -i -e "s|DB_HOST=.*|DB_HOST=${DB_HOST}|g;" ${INSTALL_CONF}
  sed -i -e "s|DB_PORT=.*|DB_PORT=${DB_PORT}|g;" ${INSTALL_CONF}
  sed -i -e "s|DB_DATABASE=.*|DB_DATABASE=${DB_DATABASE}|g;" ${INSTALL_CONF}
  sed -i -e "s|DB_USERNAME=.*|DB_USERNAME=${DB_USERNAME}|g;" ${INSTALL_CONF}
  sed -i -e "s|DB_PASSWORD=.*|DB_PASSWORD=${DB_PASSWORD}|g;" ${INSTALL_CONF}
  sed -i -e "s|DB_ROOT_PASSWORD=.*|DB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}|g;" ${INSTALL_CONF}

  # docker-compose.yml
  sed -i -e "s|DB_IMAGE|${DB_IMAGE}|g;" ${DOCKER_COMPOSE_YML}
  sed -i -e "s|DB_PATH|${DB_PATH}|g;" ${DOCKER_COMPOSE_YML}
  sed -i -e "s|DB_DATA|${DB_DATA}|g;" ${DOCKER_COMPOSE_YML}
  sed -i -e "s|DB_PORT|${DB_PORT}|g;" ${DOCKER_COMPOSE_YML}
  sed -i -e "s|DB_ROOT_PASSWORD|${DB_ROOT_PASSWORD}|g;" ${DOCKER_COMPOSE_YML}

  # Dockerfile
}

# 初始化项目数据库
DB_Init()
{
  Echo_Blue "初始化数据库..."
  # 检查容器是否正在运行
  container=${APP_NAME}-${DB_HOST}-1
  if docker ps --format "{{.Names}}" | grep -Eqi "${container}"; then
    # 进入 Docker Mysql 容器配置数据库
    # docker exec -it lnmp-mysql-1 sh -c "/data/sh/init.sh"
    docker exec -it ${container} sh -c "/data/sh/init.sh"
    Echo_Blue "添加定时任务: 备份数据库..."
    # docker exec -it lnmp-mysql-1 sh -c "/data/sh/mysql/backup.sh"
    # 这里 docker 命令不需要加 -it，因为加 -it 就要开启了一个终端，而计划任务无法进入任何终端
    if ! crontab -l | grep -Eqi "${container}"; then
      Add_Scheduled_Task "0 0 * * 0 docker exec ${container} sh \"/data/sh/backup.sh\""
    fi
  else
    Echo_Red "数据库容器 ${container} 未启动..."
  fi
}